//
//  GoodCommentCell.m
//  danpin
//
//  Created by chuangjia on 26/8/15.
//  Copyright (c) 2015 chuangjia. All rights reserved.
//

#import "GoodCommentCell.h"

#import "UIButtonView.h"
@interface GoodCommentCell ()
{
    
}
@property(nonatomic,strong)UIImageView *imagesView;
@property(nonatomic,strong)RTLabel *titleLabel;
@property(nonatomic,strong)RTLabel *timeLabel;
@property(nonatomic,strong)RTLabel *contentLabel;
@property(nonatomic,strong)UILabel *fromLabel;
@property(nonatomic,strong)UIButtonView *btnLike;
@property(nonatomic,strong)UIButtonView *btnReply;
@property(nonatomic,strong)UIImageView *lineView;

@end
@implementation GoodCommentCell

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self)
    {
        self.backgroundColor=[UIColor clearColor];
        UITapGestureRecognizer* singleRecognizer= [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleSingleTapFromUser)];
        _imagesView =[[UIImageView alloc] initWithFrame:CGRectZero];
        _imagesView.layer.cornerRadius=PHOTO_FRAME_WIDTH*2;
        _imagesView.layer.masksToBounds = YES;
        _imagesView.userInteractionEnabled=YES;
        [_imagesView addGestureRecognizer:singleRecognizer];
        [self.contentView addSubview:_imagesView];
        singleRecognizer=nil;
        
        UITapGestureRecognizer* singleRecognizer1= [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleSingleTapFromUser)];
        _titleLabel =[[RTLabel alloc] initWithFrame:CGRectMake(0,0, DEVICE_WIDTH-(PHOTO_FRAME_WIDTH+PHOTO_TIME_PADDING)*2,0)];
        _titleLabel.backgroundColor = [UIColor clearColor];
        _titleLabel.textColor = UIColorFromRGB(0x285790);
        _titleLabel.textAlignment = NSTextAlignmentLeft;
        _titleLabel.font = FONT(14);
        _titleLabel.lineSpacing =LSpacing;
        _titleLabel.userInteractionEnabled=YES;
        [_titleLabel addGestureRecognizer:singleRecognizer1];
        [self.contentView addSubview:_titleLabel];
        singleRecognizer1=nil;
        _timeLabel =[[RTLabel alloc] initWithFrame:CGRectMake(0,0, 250,20)];
        _timeLabel.backgroundColor = [UIColor clearColor];
        _timeLabel.textColor = UIColorFromRGB(0xcccccc);
        _timeLabel.textAlignment = NSTextAlignmentRight;
        _timeLabel.font = FONT(12);
        _timeLabel.lineSpacing =LSpacing;
        [self.contentView addSubview:_timeLabel];
        
        _contentLabel =[[RTLabel alloc] initWithFrame:CGRectMake(0,0, DEVICE_WIDTH-PHOTO_FRAME_WIDTH*6,0)];
        _contentLabel.backgroundColor = [UIColor clearColor];
        _contentLabel.textColor = [UIColor darkGrayColor];
        _contentLabel.textAlignment = NSTextAlignmentLeft;
        _contentLabel.font = FONT(14);
        _contentLabel.lineSpacing =LSpacing;
        [self.contentView addSubview:_contentLabel];
        
        _fromLabel =[[UILabel alloc] initWithFrame:CGRectMake(0,0, DEVICE_WIDTH-PHOTO_FRAME_WIDTH*6,0)];
        _fromLabel.backgroundColor = [UIColor clearColor];
        _fromLabel.textColor = UIColorFromRGB(0xcccccc);
        _fromLabel.textAlignment = NSTextAlignmentLeft;
        _fromLabel.font = FONT(12);
        [self.contentView addSubview:_fromLabel];
        
        _btnLike = [[UIButtonView alloc] initWithFrame:CGRectZero];
        _btnLike.backgroundColor =  [UIColor clearColor];
        __weak typeof (self) wSelf = self;
        [_btnLike setCallBack:^(BEventType t, UIView *v, id data, id key) {
            [wSelf likeAction:nil];
        }];
        [self.contentView addSubview:_btnLike];
        
        _btnReply = [[UIButtonView alloc] initWithFrame:CGRectZero];
        _btnReply.backgroundColor =  [UIColor clearColor];
        [_btnReply setCallBack:^(BEventType t, UIView *v, id data, id key) {
            [wSelf commentAction:nil];
        }];
        [self.contentView addSubview:_btnReply];
        
        _lineView =[[UIImageView alloc] initWithFrame:CGRectZero];
        _lineView.image =[UIImage imageNamed:@"line_xu.png"];
        [self.contentView addSubview:_lineView];
        
    }
    return self;
}
-(void)dealloc
{
    self.actionBlock =nil;
    self.data=nil;
    self.indexPath=nil;
    self.other=nil;
    self.key=nil;
    self.imagesView.image=nil;
    [self.imageView removeFromSuperview];
    self.imagesView=nil;
    [self.titleLabel removeFromSuperview];
    self.titleLabel=nil;
    [self.btnLike removeFromSuperview];
    self.btnLike=nil;
    [self.timeLabel removeFromSuperview];
    self.timeLabel=nil;
    [self.contentLabel removeFromSuperview];
    self.contentLabel=nil;
    [self.lineView removeFromSuperview];
    self.lineView =nil;
    //    DLog(@"%s", __PRETTY_FUNCTION__);
}
- (void)drawRect:(CGRect)rect {
    
    [super drawRect:rect];
    self.lineView.frame =CGRectMake(0,self.bounds.size.height-1, DEVICE_WIDTH, 1);
}
- (void)layoutSubviews
{
    [super layoutSubviews];
    
}
/// 根据数据模型来显示内容
- (void)showInfo:(id)model key:(id)key indexPath:(NSIndexPath *)indexPath
{
    self.key =key;
    self.indexPath=indexPath;
    NSDictionary *dict =(NSDictionary*)model;
    self.data =model;
    _imagesView.frame =CGRectMake(PHOTO_FRAME_WIDTH,PHOTO_FRAME_WIDTH, PHOTO_FRAME_WIDTH*4, PHOTO_FRAME_WIDTH*4);
    NSString * strURL =[NSString stringWithFormat:@"%@", [dict objectForKey:@"largeFace"]];
    [_imagesView sd_setImageWithURL:[NSURL URLWithString:strURL] placeholderImage:[UIImage imageNamed:@"user_face.png"]];
    NSString *test =[[NSString stringWithFormat:@"%@", [dict objectForKey:@"nick_name"]] replaceNullString];
    [_titleLabel setText:test];
    CGSize optimumSize = [_titleLabel optimumSize];
    
    _titleLabel.frame =CGRectMake(_imagesView.frame.origin.x+_imagesView.frame.size.width+PHOTO_FRAME_WIDTH, _imagesView.frame.origin.y,optimumSize.width, optimumSize.height);
    _titleLabel.text =test;
    //
    NSString *test1 =[[NSString stringWithFormat:@"%@", [dict objectForKey:@"comment_dateinfo"]] replaceNullString];
    [_timeLabel setText:test1];
    CGSize optimumSize2 = [_timeLabel optimumSize];
    _timeLabel.frame=CGRectMake(DEVICE_WIDTH-optimumSize2.width-PHOTO_FRAME_WIDTH,_titleLabel.frame.origin.y,optimumSize2.width, optimumSize2.height);
    _timeLabel.text =test1;
    //
    NSString *content =[[NSString stringWithFormat:@"%@", [dict objectForKey:@"text"]] replaceNullString];
    [_contentLabel setText:content];
    CGSize optimumSize1 = [_contentLabel optimumSize];
    
    _contentLabel.frame =CGRectMake(_imagesView.frame.origin.x+_imagesView.frame.size.width+PHOTO_FRAME_WIDTH, _titleLabel.frame.origin.y+_titleLabel.frame.size.height+PHOTO_FRAME_WIDTH, DEVICE_WIDTH-(_imagesView.frame.origin.x+_imagesView.frame.size.width+PHOTO_FRAME_WIDTH)-PHOTO_FRAME_WIDTH, optimumSize1.height);
    _contentLabel.text =content;
    //
    CGFloat y =_contentLabel.frame.origin.y+_contentLabel.frame.size.height+PHOTO_FRAME_WIDTH;
    int from = [[dict objectForKey:@"from"] intValue];
    if (from==10) {
        _fromLabel.frame =CGRectMake(_contentLabel.frame.origin.x, y,_contentLabel.frame.size.width, PHOTO_FRAME_WIDTH*2);
        NSString *titile =[NSString stringWithFormat:@"来自(求购):%@",[[dict objectForKey:@"asking"] objectForKey:@"title"]];
        _fromLabel.text =titile;
        y=y+PHOTO_FRAME_WIDTH*2;
    }
    //
    
    NSString *reply_cnt =[[NSString stringWithFormat:@"%@",[dict objectForKey:@"push_bottom"]] replaceNullString];
    if (reply_cnt.length>0&&![reply_cnt isEqualToString:@"0"]&&![reply_cnt isEqualToString:@"<null>"]) {
        [_btnReply setImage:[UIImage imageNamed:@"cai.png"] name:reply_cnt];
    }else
    {
        reply_cnt =@"0";
        [_btnReply setImage:[UIImage imageNamed:@"cai.png"] name:@"0"];
    }
    CGSize size=[reply_cnt sizeWithAttributes:@{NSFontAttributeName: [UIFont systemFontOfSize:12.0]}];
    _btnReply.frame=CGRectMake(DEVICE_WIDTH -(size.width+25)-PHOTO_FRAME_WIDTH, y, size.width+25, MORE_BUTTON_HEIGHT);
    
    int had_bottom=[[dict objectForKey:@"had_bottom"] intValue];
    if (had_bottom) {
        [_btnReply setImage:[UIImage imageNamed:@"yicai.png"] name:reply_cnt];
    }
    NSString *like_cnt =[[NSString stringWithFormat:@"%@",[dict objectForKey:@"push_top"]] replaceNullString];
    if (like_cnt.length>0&&![like_cnt isEqualToString:@"0"]&&![like_cnt isEqualToString:@"<null>"]) {
        [_btnLike setImage:[UIImage imageNamed:@"ding.png"] name:like_cnt];
    }else
    {
        like_cnt =@"0";
        [_btnLike setImage:[UIImage imageNamed:@"ding.png"] name:@"0"];
    }
    CGSize size0=[like_cnt sizeWithAttributes:@{NSFontAttributeName: [UIFont systemFontOfSize:12.0]}];
    _btnLike.frame=CGRectMake(_btnReply.frame.origin.x-(size0.width+25)-PHOTO_FRAME_WIDTH, y,size0.width+25, MORE_BUTTON_HEIGHT);
    
    int had_top=[[dict objectForKey:@"had_top"] intValue];
    if (had_top) {
        [_btnLike setImage:[UIImage imageNamed:@"yiding.png"] name:like_cnt];
    }
    [self layoutSubviews];
}
-(void)setCallBack:(void(^)(BEventType,UIView*,id,id,NSIndexPath *))callback
{
    self.actionBlock=callback;
}
-(void)likeAction:(id)sender
{
    if (self.actionBlock) {
        self.actionBlock(BEventType_ding,sender,self.data,nil,self.indexPath);
    }
}
-(void)commentAction:(id)sender
{
    if (self.actionBlock) {
        self.actionBlock(BEventType_cai,sender,self.data,nil,self.indexPath);
    }
}
-(void)handleSingleTapFrom
{
    if (self.actionBlock) {
        self.actionBlock(BEventType_comment,nil,self.data,nil,self.indexPath);
    }
}
-(void)handleSingleTapFromUser
{
    if (self.actionBlock) {
        self.actionBlock(BEventType_sunUser,nil,self.data,nil,self.indexPath);
    }
}
/// 返回Cell高度
+ (CGFloat)returnCellHeight:(id)model
{
    NSDictionary *dict =(NSDictionary *)model;
    float hight =0;
    NSString *test =[[NSString stringWithFormat:@"%@",[dict objectForKey:@"nick_name"]] replaceNullString];
    if (test.length>0) {
        test =[NSString stringWithFormat:@"%@",test];
        
        RTLabel *textLabel =[[RTLabel alloc] initWithFrame:CGRectMake(PHOTO_FRAME_WIDTH,0, DEVICE_WIDTH-(PHOTO_FRAME_WIDTH+PHOTO_TIME_PADDING)*2,0)];
        textLabel.font = FONT(14);
        textLabel.lineSpacing = LSpacing;
        [textLabel setText:test];
        CGSize optimumSize1 = [textLabel optimumSize];
        textLabel=nil;
        CGFloat hh =optimumSize1.height;
        if (hh<20) {
            hh=20;
        }
        hight =hight + hh;
    }
    NSString *text =[[NSString stringWithFormat:@"%@", [dict objectForKey:@"text"]] replaceNullString];
    if (text.length>0) {
        text =[NSString stringWithFormat:@"%@",text];
        
        RTLabel *textLabel =[[RTLabel alloc] initWithFrame:CGRectMake(PHOTO_FRAME_WIDTH,0, DEVICE_WIDTH-(PHOTO_FRAME_WIDTH)*6,0)];
        textLabel.font = FONT(14);
        textLabel.lineSpacing = LSpacing;
        [textLabel setText:text];
        CGSize optimumSize1 = [textLabel optimumSize];
        textLabel=nil;
        CGFloat hh =optimumSize1.height;
        if (hh<20) {
            hh=20;
        }
        hight =hight + hh;
    }
    
    int from = [[dict objectForKey:@"from"] intValue];
    if (from==10) {
        return PHOTO_FRAME_WIDTH*6+hight+MORE_BUTTON_HEIGHT;
    }else{
        return PHOTO_FRAME_WIDTH*4+hight+MORE_BUTTON_HEIGHT;
    }
    
}
@end
